<template>
	<view class="sell_popup">
		<view class="close" @click="$emit('close')">
			<image :src="`${baseImageUrl}/common/close1.png`"></image>
		</view>
		<view class="title">
			<view>出售{{text}}</view>
		</view>
		<view class="content">
			<view class="total">
				<text>剩余数量:{{total}}颗</text>
				<view class="btn" @click="number=total,diff_price()">全卖</view>
			</view>
			<view class="number">
				<view class="icon" @click="reduce">
					<image :src="`${baseImageUrl}/common/js.png`"></image>
				</view>
				<input type="text" placeholder="出售数量" v-model="number" @input="change_number">
				<view class="icon" @click="add">
					<image :src="`${baseImageUrl}/common/zj.png`"></image>
				</view>
			</view>
			<view class="price_input">
				<input type="text" placeholder="请输入价格" v-model="price" @input="change">
			</view>
			<view class="price">预计可得：￥{{total_price}}</view>
		</view>
		<view class="btn_view">
			<view class="cancel" @click="$emit('close')">取消</view>
			<view class="confirm" @click="submit">确定</view>
		</view>
	</view>
</template>

<script>
	import {
		userSellSell
	} from '@/request/api.js';
	export default {
		props: {
			total: {
				type: [String, Number],
				default: ""
			},
			text: {
				type: String,
				default: ""
			},
			field: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				number: "",
				price: "",
				total_price: ""
			}
		},
		onLoad() {},
		methods: {
			reduce() {
				if (this.number == 0) {
					this.$u.toast("不可减少了");
					return false;
				}
				this.number--;
				this.diff_price();
			},
			add() {
				if (this.number >= this.total) {
					this.$u.toast("不可增加了");
					return false;
				}
				this.number++;
				this.diff_price();
			},
			diff_price() {
				this.total_price = (Number(this.number) * Number(this.price)).toFixed(4);
			},
			change_number(e) {
				e.target.value = e.target.value.replace(/^0|[^\d]|[.]/g, "");
				this.$nextTick(() => {
					this.number = e.target.value;
				});
				this.diff_price();
			},
			change(e) {
				let price = e.target.value.replace(/[^\d.]/g, '').replace(/(\d)(\.)(\d*)(\2*)(\d*)/, '$1$2$3$5').replace(
					/^(\-)*(\d+)\.(\d{1,4}).*$/, '$1$2.$3');
				this.$nextTick(() => {
					this.price = price;
				});
				this.diff_price();
			},
			submit() {
				if (this.number <= 0) {
					this.$u.toast("请输入数量");
					return false;
				}
				if (this.price <= 0) {
					this.$u.toast("请输入价钱");
					return false;
				}
				uni.$u.throttle(async () => {
					await userSellSell({
						number: this.number,
						price: this.price,
						field: this.field
					});
					this.$u.toast("操作成功");
					setTimeout(() => {
						this.$emit('close');
					}, 1000);
				}, 1200);
			}
		}
	}
</script>

<style scoped lang="scss">
	.sell_popup {
		width: 630rpx;
		height: 630rpx;
		padding: 42rpx 0 52rpx;
		box-sizing: border-box;
		background: url(#{$baseImageUrl}/bg/630_630.png) no-repeat;
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		position: relative;

		.close {
			width: 122rpx;
			height: 122rpx;
			position: absolute;
			right: 0rpx;
			top: 0rpx;
			transform: translate(50rpx, -50rpx);
			z-index: 2;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			display: flex;
			justify-content: center;
			align-items: center;

			view {
				width: 256rpx;
				height: 30rpx;
				line-height: 30rpx;
				text-align: center;
				color: #494338;
				font-size: 42rpx;
				font-weight: 400;
				font-family: LiSu;
				background: url(#{$baseImageUrl}/home/title.png) no-repeat;
				background-size: 100% 100%;
			}
		}

		.content {
			flex: 1;
			padding: 30rpx 0;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.total {
				display: flex;
				justify-content: center;
				align-items: center;

				text {
					color: #494338;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
				}

				.btn {
					width: 92rpx;
					height: 44rpx;
					line-height: 44rpx;
					text-align: center;
					color: #756768;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
					margin-left: 16rpx;
					background: url(#{$baseImageUrl}/common/btn4.png) no-repeat;
					background-size: 100% 100%;
				}
			}

			.price_input {
				height: 80rpx;
				padding: 0 115rpx;
				box-sizing: border-box;
				display: flex;

				input {
					flex: 1;
					height: 80rpx;
					color: #494338;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
					border-radius: 10rpx;
					box-shadow: 2rpx -1rpx 10rpx 0px rgba(3, 0, 0, 0.2);
					padding-left: 40rpx;
					box-sizing: border-box;
					background: #C0B5B6;
				}
			}

			.number {
				height: 80rpx;
				// margin: 36rpx 0 60rpx;
				// margin: 18rpx 0 30rpx;
				padding: 0 35rpx;
				box-sizing: border-box;
				display: flex;

				.icon {
					width: 80rpx;
					height: 80rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				input {
					flex: 1;
					height: 80rpx;
					color: #494338;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
					border-radius: 10rpx;
					box-shadow: 2rpx -1rpx 10rpx 0px rgba(3, 0, 0, 0.2);
					padding-left: 40rpx;
					box-sizing: border-box;
					background: #C0B5B6;
				}
			}

			.price {
				text-align: center;
				color: #494338;
				font-size: 28rpx;
				font-weight: 400;
				font-family: SimSun;
			}
		}

		.btn_view {
			width: 100%;
			display: flex;
			justify-content: center;

			view {
				width: 210rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 30rpx;
				font-weight: 400;
				margin: 0 16rpx;
			}

			.cancel {
				background: url(#{$baseImageUrl}/common/btn2.png) no-repeat;
				background-size: 100% 100%;
			}

			.confirm {
				background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
				background-size: 100% 100%;
			}
		}
	}
</style>